import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qgchat/common/action/verify/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/io.dart';
import 'package:qgchat/http/models/ChatRecordListModel.dart';
import 'package:qgchat/http/models/ReflectRecordModel.dart';
import 'package:qgchat/http/models/TalkRecordModel.dart';
import 'package:qgchat/http/models/UserAcitvtyModel.dart';
import 'package:qgchat/http/models/addTagModel.dart';
import 'package:qgchat/http/models/allgiftModel.dart';
import 'package:qgchat/http/models/anchorInfoModel.dart';
import 'package:qgchat/http/models/anchorModel.dart';
import 'package:qgchat/http/models/appointmentListModel.dart';
import 'package:qgchat/http/models/assessModel.dart';
import 'package:qgchat/http/models/attentionModel.dart';
import 'package:qgchat/http/models/blackListModel.dart';
import 'package:qgchat/http/models/buyvideoModel.dart';
import 'package:qgchat/http/models/callRecordModel.dart';
import 'package:qgchat/http/models/fansModel.dart';
import 'package:qgchat/http/models/followModel.dart';
import 'package:qgchat/http/models/getalipayModel.dart';
import 'package:qgchat/http/models/goldRecordModel.dart';
import 'package:qgchat/http/models/goodsModel.dart';
import 'package:qgchat/http/models/intodetailsModel.dart';
import 'package:qgchat/http/models/rechargeRecordModel.dart';
import 'package:qgchat/http/models/reflectInfoModel.dart';
import 'package:qgchat/http/models/lovevideModel.dart';
import 'package:qgchat/http/models/releaseVideoModel.dart';
import 'package:qgchat/http/models/tagModel.dart';
import 'package:qgchat/http/models/userInfoModel.dart';

class UserApis {
  /// 登录
  static Future login(String tel, String psw) async {
    if (tel.isEmpty) {
      showToast("请输入手机号");
      return null;
    }
    if (psw.isEmpty) {
      showToast("请输入密码");
      return null;
    }
    if (!Verify.isChinaPhoneLegal(tel)) {
      showToast("手机号格式错误");
      return null;
    }
    return await Io.post("login", data: {"tel": tel, "password": psw});
  }

  /// 微信登录
  static Future wxLogin(
    String nickname,
    String openid,
    String head,
    String onlyset,
    String invationToken,
  ) async {
    return await Io.post('member/appLogin', data: {
      'nickname': nickname,
      'openid': openid,
      'head': head,
      'onlyset': onlyset,
      'invation_token': invationToken,
    });
  }

  /// 验证码登录
  static Future PINLogin(String tel, String code) async {
    return await Io.post('member/codelogin', data: {
      'tel': tel,
      'code': code,
    });
  }

  /// 用户信息
  static Future myInfo() async {
    Map result = await Io.get("Mysetting/Myinfo");
    if (_checkResult(result)) {
      return UserInfoModel.fromJson(result['data']);
    } else {
      return null;
    }
  }

  /// 我关注的主播
  static Future followMan() async {
    Map result = await Io.get("Mysetting/follow_man");
    if (_checkResult(result)) {
      return result['data'].map<FollowModel>((item) {
        return FollowModel.fromJson(item);
      }).toList();
    } else {
      return List<FollowModel>();
    }
  }

  /// 预约列表
  static Future appointmentList({int page = 1, int limit = 10}) async {
    Map result = await Io.get("appointment/appointmentList",
        data: {'page': page, 'limit': limit});
    if (_checkResult(result)) {
      return result['data'].map<AppointmentListModel>((item) {
        return AppointmentListModel.fromJson(item);
      }).toList();
    } else {
      return List<AppointmentListModel>();
    }
  }

  /// 喜欢的视频列表
  static Future lovevideo({int page = 1, int limit = 10}) async {
    Map result =
        await Io.get("vedio/likeVedio", data: {'page': page, 'limit': limit});
    if (_checkResult(result)) {
      return result['data'].map<LovevideoModel>((item) {
        return LovevideoModel.fromJson(item);
      }).toList();
    } else {
      return List<LovevideoModel>();
    }
  }

  /// 购买的视频列表
  static Future buyvide({int page = 1, int limit = 10}) async {
    Map result =
        await Io.get("vedio/buyVedio", data: {'page': page, 'limit': limit});
    if (_checkResult(result)) {
      return result['data'].map<BuyvideoModel>((item) {
        return BuyvideoModel.fromJson(item);
      }).toList();
    } else {
      return List<BuyvideoModel>();
    }
  }

  /// 发布视频列表
  static Future releaselist({int page = 1, int limit = 9999}) async {
    Map result = await Io.get(
      "Vedio/anchorvedio",
      data: {"page": page, "limit": limit},
    );
    if (_checkResult(result)) {
      return result['data'].map<ReleaseVideoModel>((item) {
        return ReleaseVideoModel.fromJson(item);
      }).toList();
    } else {
      return List<ReleaseVideoModel>();
    }
  }

  /// 用户动态
  static Future getAcitvity({int page = 1, int limit = 10}) async {
    Map result =
        await Io.get("member/userList", data: {'page': page, 'limit': limit});
    if (_checkResult(result)) {
      return result['data'].map<UserAcitvtyModel>((item) {
        return UserAcitvtyModel.fromJson(item);
      }).toList();
    } else {
      return List<UserAcitvtyModel>();
    }
  }

  /// 上传视频
  static Future uploadcopying(
    String title,
    String url,
    int ispay,
    String money,
    String time,
    String token,
    String thumbnail,
  ) async {
    return await Io.post("vedio/Vedio", data: {
      'title': title,
      'video': url,
      'payview': ispay,
      'paymoney': money,
      'video_time': time,
      'member_token': token,
      'thumbnail': thumbnail,
    });
  }

  /// 获取验证码
  static Future getPIN(String phone, BuildContext context) async {
    if (phone.isEmpty) {
      showToast('请填入手机号码');
      return null;
    }
    if (phone.length < 11) {
      showToast('请输入11位手机号码');
      return null;
    }
    if (!Verify.isChinaPhoneLegal(phone)) {
      showToast("手机号格式错误");
      return null;
    }
    return await Io.get('code', data: {"tel": phone});
  }

  /// 注册
  static Future register(
    String phone,
    String pin,
    String phoneId,
    String invationToken,
  ) async {
    if (phone.isEmpty || pin.isEmpty) {
      showToast('请填写完整信息');
      return null;
    }
    return await Io.post('register', data: {
      'tel': phone,
      'code': pin,
      'member_nickname': phone,
      'onlyset': phoneId,
      'invation_token': invationToken,
    });
  }

  /// 修改密码
  static Future editpassword(
    String phone,
    String pin,
    String pw,
    String rePw,
  ) async {
    if (phone.isEmpty || pw.isEmpty || pin.isEmpty || rePw.isEmpty) {
      showToast('请填写完整信息');
      return null;
    }
    if (pw.length < 6) {
      showToast('密码至少6位');
      return null;
    }
    if (pw != rePw) {
      showToast('两次密码输入不一致');
      return null;
    }
    return await Io.post('Mysetting/editpassword', data: {
      'tel': phone,
      'password': pw,
      'code': pin,
      'password2': rePw,
    });
  }

  /// 主播的总金额
  static Future reflectRecord(String token) async {
    Map result = await Io.post("anchor/ReflectRecord", data: {
      "member_token": token,
    });
    if (_checkResult(result)) {
      return ReflectRecordModel.fromJson(result['data']);
    } else {
      return ReflectRecordModel();
    }
  }

  /// 获取支付宝信息
  static Future getalipay() async {
    Map result = await Io.get("member/getalipay");
    if (_checkResult(result) && result['data'] != null) {
      return GetalipayModel.fromJson(result['data']);
    } else {
      return GetalipayModel();
    }
  }

  /// 填写支付宝账号姓名
  static Future comfiralipay(id, memberToken, alipayUser, alipayName) async {
    return await Io.post("member/comfiralipay", data: {
      'id': id,
      'member_token': memberToken,
      'alipay_user': alipayUser,
      'alipay_name': alipayName,
    });
  }

  /// 发起订单
  /// [type] 0微信 1支付宝
  static Future payment(String goodId, String type) async {
    return await Io.post('Payment/payment', data: {
      'good_id': goodId,
      'type': type,
    });
  }

  /// 填写提现信息
  static Future reflect(memberToken, count, name, money, type) async {
    return await Io.post("anchor/Reflect", data: {
      'member_token': memberToken,
      'count': count,
      'name': name,
      'money': money,
      'type': type,
    });
  }

  /// 商品列表
  static Future goods() async {
    Map result = await Io.get("Alipay/goods");
    if (_checkResult(result)) {
      return result['data'].map<GoodsModel>((item) {
        return GoodsModel.fromJson(item);
      }).toList();
    } else {
      return List<GoodsModel>();
    }
  }

  /// 获取七牛云token
  static Future getQLYToken() async {
    return await Io.post('vedio/upload');
  }

  /// 更改用户头像
  static Future editavatar(imgUrl) async {
    return await Io.post('Mysetting/editavatar', data: {
      'member_head': imgUrl,
    });
  }

  /// 获取粉丝列表
  static Future getFansList() async {
    Map result = await Io.get("Mysetting/fans");
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<FansModel>((item) {
        return FansModel.fromJson(item);
      }).toList();
    } else {
      return FansModel();
    }
  }

  /// 获取关注列表
  static Future getAttentionList() async {
    Map result = await Io.get("Mysetting/follow_man");
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<AttentionModel>((item) {
        return AttentionModel.fromJson(item);
      }).toList();
    } else {
      return AttentionModel();
    }
  }

  /// 当自己是主播获取主播信息
  static Future getAnchorInfo(memberToken) async {
    Map result = await Io.get("anchor/userInfo", data: {
      'member_token': memberToken,
    });
    if (_checkResult(result) && result['data'] != null) {
      return AnchorInfoModel.fromJson(result['data']);
    } else {
      return AnchorInfoModel();
    }
  }

  /// 其他人的主播信息
  static Future topbigVintroduce(memberToken) async {
    Map result = await Io.get("index/topbigVintroduce", data: {
      'member_token': memberToken,
    });
    if (_checkResult(result) && result['data'] != null) {
      return AnchorInfoModel.fromJson(result['data']);
    } else {
      return AnchorInfoModel();
    }
  }

  /// 主播tag
  static Future getTag() async {
    Map result = await Io.get("member/xiangxinag_tag");
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<TagModel>((item) {
        return TagModel.fromJson(item);
      }).toList();
    } else {
      return List<TagModel>();
    }
  }

  /// 修改昵称
  static Future setName(String nickname) async {
    return await Io.post('Mysetting/editnickname', data: {
      'nickname': nickname,
    });
  }

  /// 获取充值记录
  static Future getRechargeRecord(String id) async {
    Map result = await Io.get('member/rechargeRecord', data: {
      'id': id,
    });
    if (_checkResult(result)) {
      return result['data'].map<RechargeRecordModel>((item) {
        return RechargeRecordModel.fromJson(item);
      }).toList();
    } else {
      return List<RechargeRecordModel>();
    }
  }

  /// 主播详情
  static Future bigVInfo(String memberToken) async {
    Map result = await Io.get("index/bigVintroduce",
        data: {'member_token': memberToken});
    print(result['data']['receiveCount']);
    if (_checkResult(result) && result['data'] != null) {
      return AnchorModel.fromJson(result['data']);
    } else {
      return AnchorModel();
    }
  }

  /// 所有礼物
  static Future allgift(String memberToken) async {
    Map result =
        await Io.get("member/allgift", data: {'member_token': memberToken});
    if (_checkResult(result)) {
      result['data']['sum'] = int.parse(result['data']['sum'].toString());
      return AllgiftModel.fromJson(result['data']);
    } else {
      return AllgiftModel();
    }
  }

  /// 所有标签
  static Future allyingxiang(int id) async {
    Map result = await Io.get("member/allyingxiang", data: {'zhubo_id': id});
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<AllTagModel>((item) {
        return AllTagModel.fromJson(item);
      }).toList();
    } else {
      return List<AllTagModel>();
    }
  }

  /// 关注主播
  static Future follow(int id) async {
    return await Io.get("member/follow", data: {'follow_id': id});
  }

  /// 取消关注主播
  static Future cancelfollow(int id) async {
    return await Io.get("member/cancelfollow", data: {'follow_id': id});
  }

  /// 用户购买小视频
  static Future buyvideo(int id, String token) async {
    return await Io.post("vedio/buyvediorevord",
        data: {'vedio_id': id, 'member_token': token});
  }

  /// 获取通话列表
  static Future getCallRecordLists(String token, int page, int limit) async {
    Map<String, dynamic> data = {
      'token': token,
      'page': page,
      'limit': limit,
    };
    Map result = await Io.get('Invitation/Invitationlist', data: data);
    if (_checkResult(result)) {
      return result['data'].map<CallRecordModel>((item) {
        return CallRecordModel.fromJson(item);
      }).toList();
    } else {
      return List<CallRecordModel>();
    }
  }

  /// 拉黑列表
  static Future blackList() async {
    Map result = await Io.get("member/blackList");
    if (_checkResult(result)) {
      return result['data'].map<BlackListModel>((item) {
        return BlackListModel.fromJson(item);
      }).toList();
    } else {
      return List<BlackListModel>();
    }
  }

  /// 拉黑与拉白
  /// 默认拉黑 1拉黑 0解除
  static Future black(int id, {int type = 1}) async {
    return await Io.post("member/black", data: {
      'id': id,
      'type': type,
    });
  }

  /// 获取金币记录列表
  static Future getGoldRecordList(String token, int page, int limit) async {
    Map<String, dynamic> data = {
      'member_token': token,
      'page': page,
      'limit': limit,
      'type': 2,
    };
    Map result = await Io.get('member/incomedetail', data: data);
    if (_checkResult(result)) {
      return result['data'].map<GoldRecordModel>((item) {
        return GoldRecordModel.fromJson(item);
      }).toList();
    } else {
      return List<GoldRecordModel>();
    }
  }

  /// 获取评价记录列表
  static Future getAssessRecordsLists(String token, int page, int limit) async {
    Map<String, dynamic> data = {
      'token': token,
      'page': page,
      'limit': limit,
    };
    Map result = await Io.get('anchor/allcomment', data: data);
    if (_checkResult(result)) {
      return result['data'].map<AssessModel>((item) {
        return AssessModel.fromJson(item);
      }).toList();
    } else {
      return List<AssessModel>();
    }
  }

  /// 邀请明细(我邀请了那些人)
  static Future intodetails(String token) async {
    Map result = await Io.get('member/intodetails', data: {
      'member_token': token,
    });
    if (_checkResult(result)) {
      return result['data'].map<IntodetailsModel>((item) {
        return IntodetailsModel.fromJson(item);
      }).toList();
    } else {
      return List<IntodetailsModel>();
    }
  }

  /// 邀请收益(我从邀请的人以及其下级中获取的收益)
  static Future secondintodetails(String token) async {
    Map result = await Io.get('member/secondintodetails', data: {
      'member_token': token,
    });
    if (_checkResult(result)) {
      return result['data'].map<IntodetailsModel>((item) {
        return IntodetailsModel.fromJson(item);
      }).toList();
    } else {
      return List<IntodetailsModel>();
    }
  }

  /// 填写邀请码
  static Future sharehref(String token, String code) async {
    return await Io.post(
      'member/sharehref',
      data: {
        'member_token': token,
        'invitationcode': code,
      },
      showError: false,
    );
  }

  /// 提现明细
  static Future reflectInfo(String token,
      {int page = 1, int limit = 10}) async {
    Map result = await Io.get('member/ReflectInfo', data: {
      'member_token': token,
      'page': page,
      'limit': limit,
    });
    if (_checkResult(result)) {
      return result['data'].map<ReflectInfoModel>((item) {
        return ReflectInfoModel.fromJson(item);
      }).toList();
    } else {
      return List<ReflectInfoModel>();
    }
  }

  /// 判断是否填写了邀请码
  static Future<bool> isinvitationcode(String token) async {
    Map result = await Io.post(
      'member/isinvitationcode',
      data: {
        'member_token': token,
      },
      showError: false,
    );
    return result['status'] == 1;
  }

  /// 注册日志
  static Future registerLog(Map<String, dynamic> data) async {
    return await Io.post(
      'log/register',
      data: data,
      showError: false,
    );
  }

  /// 登录日志
  static Future loginLog(Map<String, dynamic> data) async {
    return await Io.post(
      'log/login',
      data: data,
      showError: false,
    );
  }

  /// 聊天记录
  static Future<List<ChatRecordListModel>> recordList(String token) async {
    Map result = await Io.get(
      'member/recordList',
      data: {'token': token},
    );
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<ChatRecordListModel>((item) {
        return ChatRecordListModel.fromJson(item);
      }).toList();
    } else {
      return List<ChatRecordListModel>();
    }
  }

  /// 点对点聊天记录
  static Future<List<TalkRecordModel>> talkRecord(
    String token,
    String other, {
    int id,
  }) async {
    Map<String, dynamic> data = {'token': token, 'other': other};
    if (id != null) data['id'] = id;
    Map result = await Io.get(
      'member/talkRecord',
      data: data,
    );
    if (_checkResult(result) && result['data'] != null) {
      return result['data'].map<TalkRecordModel>((item) {
        return TalkRecordModel.fromJson(item);
      }).toList();
    } else {
      return List<TalkRecordModel>();
    }
  }

  /// 查询主播自己的微信
  static Future<String> getWechat() async {
    Map result = await Io.get('anchor/getWechat');
    return result['data'];
  }

  /// 设置主播的微信
  static Future setWechat(String wechat) async {
    return await Io.post(
      'anchor/setWechat',
      data: {'wechat': wechat},
    );
  }

  // 设置主播的封面视频
  static Future coverVideo(String video) async {
    return await Io.post(
      'anchor/coverVideo',
      data: {'video': video},
    );
  } 

  /// 查询该用户和主播之间的亲密度 以及主播微信等详情信息
  static Future userGetAnchorWechat(
    String userToken,
    String anchorToken,
  ) async {
    Map result = await Io.get(
      'anchor/userGetAnchorWechat',
      data: {
        'userToken': userToken,
        'anchorToken': anchorToken,
      },
    );
    return result['data'];
  }

  /// 礼物详情
  static Future giftInfo(int id) async {
    Map result = await Io.get(
      'gift/giftInfo',
      data: {'id': id},
    );
    return result['data'];
  }
}

/// 检测回调数据是否可用
_checkResult(res) {
  return res != null && res['status'] == 1;
}
